<?php

!defined('IN') && exit('Access denied');

$opt = $_REQUEST['opt'];

$allow = array('list','view','add');

$opt = in_array($opt,$allow) ? $opt : $allow[0];

$page_no = (int)$_GET['page_no'];
$bid = $_GET['bid'];

if ($opt == 'list') 
{
    if ($bid) {
        $baoxiu = $_ENV['db']->fetch("SELECT * FROM baoxiu WHERE customer_id = '{$_G['uid']}' AND bid = '{$bid}'");
        if (!$baoxiu) exit('bx_not_found');

        $template->baoxiu = $baoxiu;
        $template->my_page = 'front/baoxiu_view';
        $template->page_no = $page_no;
        $template->title = '保修单详情 - 会员中心';
    } else {
        $total = $_ENV['db']->fetchCol("SELECT count(bid) as total FROM `baoxiu` WHERE customer_id = '{$_G['uid']}'"); 
        $pager = new class_pager();
        $pager->setConfig($page_no,$total,40);
        $pager->setUrl('my.php?act=baoxiu&opt=list&page_no=tpt','tpt');

        $baoxius = $_ENV['db']->fetchAll("SELECT * FROM baoxiu WHERE `customer_id` = '{$_G['uid']}' order by bid desc LIMIT ".$pager->getLimit()." OFFSET ".$pager->getOffset());

        $template->baoxius = $baoxius;

        $template->pager = $pager->output('page_num');
        $template->page_no = $page_no;
        $template->my_page = 'front/baoxiu_list';
        $template->title = '保修单列表 - 会员中心';
    }
} 
else if($opt == 'add') 
{
    if (POST && AJAX) 
	{
        $data = getRequests(array('title','content'));
        if (count($data) !=2 )
		{
            outputJson(0,'请补全信息');
        }
        if (strlen($data['title']) > 100) {
            outputJson(0,'保修单名称不能超过100个字符');
        }
		$data['title'] = $data['title'];
        $data['content'] = htmlspecialchars($data['content']);
        $data['created_time'] = time();
        $data['customer_id']   = $_G['uid'];         //领导人ID
        $data['customer_name'] = $_G['username'];   //领导人名称
		$data['audit_state'] = '1';                 //状态为未审核
        $sql = "INSERT INTO baoxiu ".buildSql($data);	
        $_ENV['db']->runSql($sql);
        outputJson(1,'操作成功');
    }
    $template->my_page = 'front/baoxiu_add';
    $template->title = '填写保修单 - 会员中心';
}